/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entities.Article;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author AMENOSA
 */
public class ArticleModel extends MyConfig {

    public ArticleModel() {
        super();
    }

    public Iterator getAllArticle() {
        try {
            openConnect();
            ArrayList list = new ArrayList();
            CallableStatement cst = conn.prepareCall("{call getAllArticle}");
            ResultSet rs = cst.executeQuery();
            while (rs.next()) {
                Article article = new Article();
                article.setId(rs.getInt("Article_id"));
                article.setCategory_id(rs.getInt("Category_id"));
                article.setTitle(rs.getString("Article_title"));
                article.setContent(rs.getString("Article_content"));
                article.setRate(rs.getFloat("Rate"));
                article.setStatus(rs.getBoolean("Status"));
                list.add(article);
            }
            closeConnect();
            return list.iterator();
        } catch (SQLException ex) {
        }
        return null;
    }

    public Iterator getArticleList(int page, int pageSize) {
        try {
            ArrayList list = new ArrayList();
            openConnect();
            CallableStatement cst = conn.prepareCall("{call spSeparationPage_Article(?,?)}");
            cst.setInt(1, page);
            cst.setInt(2, pageSize);
            ResultSet rs = cst.executeQuery();
            while (rs.next()) {
                Article article = new Article();
                article.setId(rs.getInt("Article_id"));
                article.setCategory_id(rs.getInt("Category_id"));
                article.setTitle(rs.getString("Article_title"));
                article.setContent(rs.getString("Article_content"));
                article.setRate(rs.getFloat("Rate"));
                article.setStatus(rs.getBoolean("Status"));
                list.add(article);
            }
            closeConnect();
            return list.iterator();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public int getPageCount(int pageSize) {
        try {
            int result = 0, count = 0;
            openConnect();
            ResultSet rs = getResultset("Select Count=Count(*) from Article");
            if (rs.next()) {
                count = rs.getInt("Count");
            }
            closeConnect();
            int temp = count % pageSize;
            if (temp != 0) {
                result = count / pageSize + 1;
            } else {
                result = count / pageSize;
            }
            return result;
        } catch (SQLException ex) {
        }
        return 0;
    }

    public int updateArticle(int category_id, String title, String content, float rate, boolean status, int id) {
        int update = -1;
        try {
            openConnect();
            CallableStatement cst = conn.prepareCall("{call update_Article(?,?,?,?,?,?)}");
            cst.setInt(1, category_id);
            cst.setString(2, title);
            cst.setString(3, content);
            cst.setFloat(4, rate);
            cst.setBoolean(5, status);
            cst.setInt(6, id);
            update = cst.executeUpdate();
            closeConnect();
        } catch (SQLException ex) {
        }
        return update;
    }

    public int insertArticle(int category_id, String title, String content, float rate, boolean status) {
        int insert = -1;
        try {
            openConnect();
            CallableStatement cst = conn.prepareCall("{call insert_Article(?,?,?,?,?)}");
            cst.setInt(1, category_id);
            cst.setString(2, title);
            cst.setString(3, content);
            cst.setFloat(4, rate);
            cst.setBoolean(5, status);
            insert = cst.executeUpdate();
            closeConnect();
        } catch (SQLException ex) {
        }
        return insert;
    }

    public int deleteNArticle(String id) {
        int rs = -1;
        try {
            openConnect();
            String sql = "Delete Article Where Article_id in (" + id + ")";
            Statement st = conn.createStatement();
            rs = st.executeUpdate(sql);
            closeConnect();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;
    }

    public int deleteArticle(int id) {
        int delete = -1;
        try {
            openConnect();
            CallableStatement cst = conn.prepareCall("{call delete_Article(?) }");
            cst.setInt(1, id);
            delete = cst.executeUpdate();
            closeConnect();
        } catch (SQLException ex) {
        }
        return delete;
    }

    public Article getArticle(int id) {
        try {
            openConnect();
            PreparedStatement pst = conn.prepareStatement("Select * From Article Where Article_id=?");
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            Article article = new Article();
            if (rs.next()) {
                article.setId(rs.getInt("Article_id"));
                article.setCategory_id(rs.getInt("Category_id"));
                article.setTitle(rs.getString("Article_title"));
                article.setContent(rs.getString("Article_content"));
                article.setRate(rs.getFloat("Rate"));
                article.setStatus(rs.getBoolean("Status"));
            }
            closeConnect();
            return article;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
